package org.zero.controller;

import java.time.LocalDateTime;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.zero.entity.AjaxQuery;
import org.zero.entity.Result;

/**
 * @author cnzeropro@qq.com
 * @date 2021/6/26
 */
@RestController
@RequestMapping("ajax")
public class AjaxController {

    @RequestMapping("test")
    public Result ajaxTest(AjaxQuery query) {
        System.out.println("任何请求");
        System.out.println("前端上传的参数：" + query);

        MultipartFile multipartFile = query.getFile();
        System.out.println("文件名：" + multipartFile.getOriginalFilename());
        //没法传回前端，故设置为null
        query.setFile(null);

        Result result = Result.builder()
                .code(200)
                .message("请求成功")
                .time(LocalDateTime.now())
                .data(query)
                .build();

        System.out.println("回传给前端的数据：" + result);
        return result;
    }

    @GetMapping("get")
    public Result ajaxGet(AjaxQuery query) {
        System.out.println("get请求");
        System.out.println("前端上传的参数：" + query);

        Result result = Result.builder()
                .code(200)
                .message("GET请求成功")
                .time(LocalDateTime.now())
                .data(query)
                .build();

        System.out.println("回传给前端的数据：" + result);
        return result;
    }

    @PostMapping("post")
    public Result ajaxPost(AjaxQuery query) {
        System.out.println("post请求");
        System.out.println("前端上传的参数：" + query);

        Result result = Result.builder()
                .code(200)
                .message("POST请求成功")
                .time(LocalDateTime.now())
                .data(query)
                .build();

        System.out.println("回传给前端的数据：" + result);
        return result;
    }

    @PutMapping("put")
    public Result ajaxPut(AjaxQuery query) {
        System.out.println("put请求");
        System.out.println("前端上传的参数：" + query);

        Result result = Result.builder()
                .code(200)
                .message("PUT请求成功")
                .time(LocalDateTime.now())
                .data(query)
                .build();

        System.out.println("回传给前端的数据：" + result);
        return result;
    }

    @DeleteMapping("delete")
    public Result ajaxDelete(AjaxQuery query) {
        System.out.println("delete请求");
        System.out.println("前端上传的参数：" + query);

        Result result = Result.builder()
                .code(200)
                .message("DELETE请求成功")
                .time(LocalDateTime.now())
                .data(query)
                .build();

        System.out.println("回传给前端的数据：" + result);
        return result;
    }
}
